package com.uenpay.oem.data.modelImpl;

import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.uenpay.oem.common.TransCodeConstant;
import com.uenpay.oem.data.bean.Request;
import com.uenpay.oem.net.RetrofitHttpService;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;
import com.uenpay.service.model.IBankCardModel;
import com.uenpay.util.net.http.BaseRequest;
import com.uenpay.util.net.http.IHttpManager;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;
import com.uenpay.util.net.retrofit.HttpManager;
import com.uenpay.util.net.retrofit.RetrofitRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

/**
 * Author: liulou
 * Date: 2017/6/6.
 * desc:
 */
@Route(path = "/service/backcard")
public class BankCardModel implements IBankCardModel {

    private IHttpManager manager = null;
    private BaseRequest getUserBankListReq = null;
    private BaseRequest userBankCardBinReq = null;
    private BaseRequest relieveUserBankReq = null;
    private BaseRequest saveUserBankReq = null;
    private BaseRequest recognitionUserBankReq = null;
    private BaseRequest defaultUserBankDetailReq = null;
    private BaseRequest getTransBankDetailReq = null;


    public BankCardModel() {
        this.manager = HttpManager.getInstance(RetrofitHttpService.class);
    }

    @Override
    public void init(Context context) {

    }

    @Override
    public void getUserBankList(HttpOnNextListener<List<BankCardInfo>> listener) {
        if (manager == null) {
            return;
        }
        getUserBankListReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_GET_USER_BANK_LIST);
                return ((RetrofitHttpService) methods).getBankCardList(request);
            }
        };
        manager.doHttpDeal(getUserBankListReq);
    }

    @Override
    public void userBankCardBinSelect(final String cardNo, HttpOnNextListener<BankCardInfo> listener) {
        if (manager == null) {
            return;
        }
        userBankCardBinReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("cardNo", cardNo);
                Request request = new Request(TransCodeConstant.CODE_USER_BANK_CARD_BIN_SELECT, body);
                return ((RetrofitHttpService) methods).userBankCardBinSelect(request);
            }
        };
        manager.doHttpDeal(userBankCardBinReq);
    }

    @Override
    public void saveUserBank(final BankCardInfo card, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        saveUserBankReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("bankName", card.getBankName());
                body.put("bankNo", card.getBankNo());
                body.put("cardNo", card.getCardNo());
                body.put("userName", card.getUserName());
                body.put("cardType", card.getCardType());
                body.put("expireDate", card.getExpireDate());
                body.put("phone", card.getPhone());
                body.put("cvvCode", card.getCvvCode());
                Request request = new Request(TransCodeConstant.CODE_SAVE_USER_BANK, body);
                return ((RetrofitHttpService) methods).saveUserBank(request);
            }
        };
        manager.doHttpDeal(saveUserBankReq);
    }

    @Override
    public void relieveUserBankDetail(final String userBankId, final int status, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        relieveUserBankReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("userBankId", userBankId);
                body.put("openStatus", String.valueOf(status));
                Request request = new Request(TransCodeConstant.CODE_RELIEVE_USER_BANK_DETAIL, body);
                return ((RetrofitHttpService) methods).relieveUserBankDetail(request);
            }
        };
        manager.doHttpDeal(relieveUserBankReq);
    }

    @Override
    public void recognitionUserBank(final String imgBase64, HttpOnNextListener<BankCardInfo> listener) {
        if (manager == null) {
            return;
        }
        recognitionUserBankReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("photoImage", imgBase64);
                Request request = new Request(TransCodeConstant.CODE_RECOGNITION_USER_BANK, body);
                return ((RetrofitHttpService) methods).recognitionUserBank(request);
            }
        };
        manager.doHttpDeal(recognitionUserBankReq);
    }

    @Override
    public void defaultUserBankDetail(final String userBankId, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        defaultUserBankDetailReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("userBankId", userBankId);
                Request request = new Request(TransCodeConstant.CODE_DEFAULT_USER_BANK_DETAIL, body);
                return ((RetrofitHttpService) methods).defaultUserBankDetail(request);
            }
        };
        manager.doHttpDeal(defaultUserBankDetailReq);
    }

    @Override
    public void getTransBankList(final String tradeType, HttpOnNextListener<TransBankCardList<BankCardInfo>> listener) {
        if (manager == null) {
            return;
        }
        getTransBankDetailReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Map<String, String> body = new HashMap<>();
                body.put("tradeType", tradeType);
                Request request = new Request(TransCodeConstant.CODE_GET_TRANS_BANK_LIST, body);
                return ((RetrofitHttpService) methods).getTransBankList(request);
            }
        };
        manager.doHttpDeal(getTransBankDetailReq);
    }
}
